package com.nlx.notes.module.service.impl.loan;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nlx.notes.module.bean.bo.LoanDetailBO;
import com.nlx.notes.module.bean.dto.loan.LoanDetailQueryDTO;
import com.nlx.notes.module.bean.dto.loan.LoanSummarySaveDTO;
import com.nlx.notes.module.bean.dto.loan.LoanSummaryUpdateDTO;
import com.nlx.notes.module.bean.dto.loan.RepayDetailSaveDTO;
import com.nlx.notes.module.core.consts.GlobalConst;
import com.nlx.notes.module.core.enums.error.LoanCodeEnum;
import com.nlx.notes.module.core.util.AmountConverterUtils;
import com.nlx.notes.module.entity.LoanDetail;
import com.nlx.notes.module.mapper.loan.LoanDetailMapper;
import com.nlx.notes.module.service.loan.ILoanDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nlx.notes.support.handler.exception.BusinessException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nlx
 * @since 2025-10-25
 */
@Service
public class LoanDetailServiceImpl extends ServiceImpl<LoanDetailMapper, LoanDetail> implements ILoanDetailService {

    @Override
    public Boolean saveLoan(LoanSummarySaveDTO loanSummarySaveDTO) {
        LoanDetail loanDetail = new LoanDetail();
        loanDetail.setBorrowerId(loanSummarySaveDTO.getBorrowerId());
        loanDetail.setSummaryId(loanSummarySaveDTO.getSummaryId());
        loanDetail.setType(loanSummarySaveDTO.getLoanType());
        loanDetail.setAmount(loanSummarySaveDTO.getAmountInt());
        loanDetail.setLoanDate(loanSummarySaveDTO.getLoanDate());
        loanDetail.setUserId(loanSummarySaveDTO.getUserId());
        loanDetail.setFlowId(loanSummarySaveDTO.getFlowId());
        loanDetail.setAccountId(loanSummarySaveDTO.getAccountId());
        loanDetail.setRemarks(loanSummarySaveDTO.getRemark());
        loanDetail.setCreatedTime(LocalDateTime.now());
        loanDetail.setDelFlag(GlobalConst.DEL_FLAG_NORMAL);
        return this.save(loanDetail);
    }

    @Override
    public long countByCondition(LoanDetailQueryDTO loanDetailQueryDTO) {
        return this.getBaseMapper().countByCondition(loanDetailQueryDTO);
    }

    @Override
    public List<LoanDetailBO> selectPageByCondition(LoanDetailQueryDTO loanDetailQueryDTO) {
        return this.getBaseMapper().selectPageByCondition(loanDetailQueryDTO);
    }

    @Override
    public boolean updateLoanDetail(LoanSummaryUpdateDTO loanSummaryUpdateDTO) {
        LoanDetail updateLoanDetail = new LoanDetail();
        updateLoanDetail.setId(loanSummaryUpdateDTO.getId());
        updateLoanDetail.setAccountId(loanSummaryUpdateDTO.getAccountId());
        updateLoanDetail.setAmount(AmountConverterUtils.convertYuanToFen(loanSummaryUpdateDTO.getAmount()));
        updateLoanDetail.setLoanDate(loanSummaryUpdateDTO.getLoanDate());
        updateLoanDetail.setRemarks(loanSummaryUpdateDTO.getRemark());
        updateLoanDetail.setUpdatedTime(LocalDateTime.now());
        return this.updateById(updateLoanDetail);
    }

    @Override
    public LoanDetail findById(Long id,Long userId) {
        return  this.getBaseMapper().findById(id,userId);
    }

    @Override
    public Integer sumLoanDetail(Long summaryId, String type) {
        return this.getBaseMapper().sumLoanDetail(summaryId,type);
    }

    @Override
    public LoanDetail saveRepay(RepayDetailSaveDTO repayDetailSaveDTO) {
        LoanDetail loanDetail = new LoanDetail();
        loanDetail.setBorrowerId(repayDetailSaveDTO.getBorrowerId());
        loanDetail.setSummaryId(repayDetailSaveDTO.getLoanSummaryId());
        loanDetail.setAccountId(repayDetailSaveDTO.getAccountId());
        loanDetail.setType(GlobalConst.Loan.LOAN_TYPE_REPAY);
        loanDetail.setAmount(AmountConverterUtils.convertYuanToFen(repayDetailSaveDTO.getRepayAmount()));
        loanDetail.setLoanDate(repayDetailSaveDTO.getRepayDate());
        loanDetail.setUserId(repayDetailSaveDTO.getUserId());
        loanDetail.setCreatedTime(LocalDateTime.now());
        loanDetail.setUpdatedTime(LocalDateTime.now());
        loanDetail.setFlowId(repayDetailSaveDTO.getFlowId());
        loanDetail.setDelFlag(GlobalConst.DEL_FLAG_NORMAL);
        boolean save = this.save(loanDetail);
        if(save){
            return loanDetail;
        }
        throw new BusinessException(LoanCodeEnum.OPERATION_FAIL_ERROR);
    }

    @Override
    public boolean removeLoanDetail(Long id, Long userId) {
        if(null == id || null == userId){
            log.error("删除还款详情中的数据失败，id或者用户id为空");
            return false;
        }
       return  this.update(Wrappers.lambdaUpdate(LoanDetail.class)
                .set(LoanDetail::getDelFlag,GlobalConst.DEL_FLAG_REMOVE).eq(LoanDetail::getId,id)
                .set(LoanDetail::getUpdatedTime,LocalDateTime.now())
                .eq(LoanDetail::getUserId,userId)
                .eq(LoanDetail::getDelFlag,GlobalConst.DEL_FLAG_NORMAL));
    }

    @Override
    public boolean removeAllRecordBySummaryId(Long summaryId, Long userId) {
        if(null == summaryId || null == userId){
            log.error("summaryId is null or userId is null !");
            return false;
        }
        return this.remove(Wrappers.lambdaUpdate(LoanDetail.class)
                .eq(LoanDetail::getSummaryId,summaryId)
                .eq(LoanDetail::getUserId,userId));
    }

    @Override
    public List<Long> findFlowIdsBySummaryId(Long summaryId, Long userId) {
        if(null == summaryId || null == userId){
            return null;
        }
        return this.getBaseMapper().findFlowIdsBySummaryId(summaryId,userId);
    }
}
